package com.pig4cloud.ila.client.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.ila.client.api.entity.Article;
import com.pig4cloud.ila.client.service.ArticleService;
import com.pig4cloud.pig.common.core.constant.CommonConstants;
import com.pig4cloud.pig.common.core.constant.enums.ArticleStateEnum;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author KonBAI
 * @version 1.0
 * Create by 2020/11/02 12:15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/home")
@Api(value = "home", tags = "首页管理")
public class HomeController {

    private final ArticleService articleService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param article 文章管理表
     * @return
     */
    @Inner(value = false)
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/articleList")
    public R getArticlePage(Page page, Article article) {
        boolean titleFlag = ObjectUtil.isNotEmpty(article.getTitle());
        boolean authorFlag = ObjectUtil.isNotEmpty(article.getTitle());
        return R.ok(articleService.page(page, Wrappers.lambdaQuery(new Article())
                .eq(Article::getDelFlag, CommonConstants.STATUS_NORMAL)
                .eq(Article::getStateFlag, ArticleStateEnum.PASS.getCode())
                .and(titleFlag && authorFlag, i -> i
                        .like(titleFlag, Article::getTitle, article.getTitle())
                        .or()
                        .like(authorFlag, Article::getAuthor, article.getAuthor()))
                .orderByDesc(Article::getCreateTime)));
    }
}
